package com.android.mytableview.table

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.marginLeft
import androidx.core.view.marginTop
import com.android.mytableview.R
import com.android.mytableview.bean.TableBean
import com.android.mytableview.util.LetterUtil
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlin.math.roundToInt


class TableViewActivity : AppCompatActivity(){


    private var rv:SwipeRecyclerView?=null
    private var adapter:TableAdapter ?= null

//    private var ivDrag:ImageView ?= null
    private var ivDelete:ImageView ?=null
    private var rlSelect:RelativeLayout?=null
    private var ivDragSelectColumn:ImageView ?= null
    private var ivDragSelectRow:ImageView ?= null

    /**
     * 头部拖拽
     */
    private var dX = 0f
    private var dY = 0f
    private var lastX = 0f
    private var lastY = 0f // 用于存储上一次的位置

    /**
     * 选中内容拖拽
     */
    private var rlDx = 0f
    private var rlDy = 0f
    private var rlLastX = 0f
    private var rlLastY = 0f


    private var rvWidth = 600
    private var rvHeight = 600
    private var row = 4 + 1 + 1
    private var column = 4 + 1 + 1

    private var list : MutableList<TableBean> = arrayListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_table_view)

//        initIvDrag()
        initIvDelete()
        initRlSelect()
        initRv()

    }

    private fun initRv(){
        rv = findViewById(R.id.rv)
        rv?.let {rv->
            rv.isNestedScrollingEnabled = false
            var rvParams = rv.layoutParams
            rvParams.width = rvWidth
            rvParams.height = rvHeight
            rv.layoutParams = rvParams

            adapter = TableAdapter(
                this, arrayListOf(),rvWidth,rvHeight,row,column
            )
            adapter?.let {
                //设置布局管理器
                val flexboxLayoutManager = FlexboxLayoutManager(this)
                //flexDirection 属性决定主轴的方向（即项目的排列方向）。类似 LinearLayout 的 vertical 和 horizontal。
                flexboxLayoutManager.flexDirection = FlexDirection.ROW //主轴为水平方向，起点在左端。
                //flexWrap 默认情况下 Flex 跟 LinearLayout 一样，都是不带换行排列的，但是flexWrap属性可以支持换行排列。
                flexboxLayoutManager.flexWrap = FlexWrap.WRAP //按正常方向换行
                //justifyContent 属性定义了项目在主轴上的对齐方式。
                flexboxLayoutManager.justifyContent =
                    JustifyContent.FLEX_START //交叉轴的起点对齐。

                rv.layoutManager = flexboxLayoutManager
                rv.setHasFixedSize(false)
                rv.adapter = it
                rv.isNestedScrollingEnabled = false

                //选中列
                it.setOnSelectColumnItem { isSelect,width, height, left, top ->
                    Log.e("setOnSelectColumnItem","isSelect=$isSelect width=$width height=$height left=$left top=$top")
//                    ivDrag?.let { ivDrag->
//                        ivDrag.setBackgroundResource(R.drawable.ic_add_column_width)
//                        if (isSelect){
//                            ivDrag.visibility = View.VISIBLE
//                        }else{
//                            ivDrag.visibility = View.GONE
//                        }
//                        val ivWidth = 40
//                        val ivHeight = 30
//                        var ivParams:FrameLayout.LayoutParams = ivDrag.layoutParams as FrameLayout.LayoutParams
//                        ivParams.width = ivWidth
//                        ivParams.height = ivHeight
//                        val ivLeft = left + width - ivWidth/2
//                        val ivTop = top + (height - ivHeight) / 2
//                        ivParams.setMargins(ivLeft,ivTop,0,0)
//                        ivDrag.layoutParams = ivParams
//                    }

                    ivDelete?.let { ivDelete->
                        if (isSelect){
                            ivDelete.visibility = View.VISIBLE
                        }else{
                            ivDelete.visibility = View.GONE
                        }
                        val ivBottomMargin = 5
                        var ivLeftMargin = left
                        val ivWidth = (height * 4 / 5f).toInt()
                        var ivParams:FrameLayout.LayoutParams = ivDelete.layoutParams as FrameLayout.LayoutParams
                        ivParams.width = ivWidth
                        ivParams.height = ivWidth
                        if (ivWidth <= width){
                            ivLeftMargin = ((width - ivWidth) / 2f + left).toInt()
                        }else{
                            ivLeftMargin = (left - (ivWidth - width) / 2f).toInt()
                        }

                        ivParams.setMargins(ivLeftMargin,top,0,ivBottomMargin)
                        ivDelete.layoutParams = ivParams

                        ivDelete.post {
                            val rvTopMargin = top+ivDelete.measuredHeight + ivBottomMargin
                            var rvParams : FrameLayout.LayoutParams = rv.layoutParams as FrameLayout.LayoutParams
                            if (isSelect){
                                rvParams.setMargins(0,rvTopMargin,0,0)
                            }else{
                                rvParams.setMargins(0,0,0,0)
                            }
                            rv.layoutParams = rvParams

//                            ivDrag?.let { ivDrag->
//                                var ivParams:FrameLayout.LayoutParams = ivDrag.layoutParams as FrameLayout.LayoutParams
//                                val ivTopMargin = ivParams.topMargin + ivDelete.measuredHeight + ivBottomMargin
//                                ivParams.topMargin = ivTopMargin
//                                ivDrag.layoutParams = ivParams
//                            }

                        }

                    }

                    rlSelect?.let { rlSelect->
                        ivDragSelectColumn?.visibility = View.VISIBLE
                        ivDragSelectRow?.visibility = View.GONE
                        if (isSelect){
                            rlSelect.visibility = View.VISIBLE
                        }else{
                            rlSelect.visibility = View.GONE
                        }
                        rv.post {
                            val tableRange = it.getSelectColumnRange()
                            val rlWidth = 40
                            val rlHeight = tableRange.height
                            val rlLeft = left + width - rlWidth/2
                            val rlTop = rv.marginTop
                            var rlParams:FrameLayout.LayoutParams = rlSelect.layoutParams as FrameLayout.LayoutParams
                            rlParams.width = rlWidth
                            rlParams.height = rlHeight
                            rlParams.setMargins(rlLeft,rlTop,0,0)
                            rlSelect.layoutParams = rlParams
                        }
                    }
                }

                //选中行
                it.setOnSelectRowItem { isSelect, width, height, left, top ->
//                    ivDrag?.let { ivDrag->
//                        ivDrag.setBackgroundResource(R.drawable.ic_add_row_height)
//                        if (isSelect){
//                            ivDrag.visibility = View.VISIBLE
//                        }else{
//                            ivDrag.visibility = View.GONE
//                        }
//                        val ivWidth = 30
//                        val ivHeight = 40
//                        var ivParams:FrameLayout.LayoutParams = ivDrag.layoutParams as FrameLayout.LayoutParams
//                        ivParams.width = ivWidth
//                        ivParams.height = ivHeight
//                        val ivLeft = left + width/2 - ivWidth/2
//                        val ivTop = top + height - ivHeight / 2
//                        ivParams.setMargins(ivLeft,ivTop,0,0)
//                        ivDrag.layoutParams = ivParams
//                    }

                    ivDelete?.let { ivDelete->
                        if (isSelect){
                            ivDelete.visibility = View.VISIBLE
                        }else{
                            ivDelete.visibility = View.GONE
                        }
                        val ivLeftMargin = 5
                        var ivTopMargin = top
                        val ivWidth = (width * 4 / 5f).toInt()
                        val ivHeight = (width * 4 / 5f).toInt()
                        var ivParams:FrameLayout.LayoutParams = ivDelete.layoutParams as FrameLayout.LayoutParams
                        ivParams.width = ivWidth
                        ivParams.height = ivHeight
                        if (ivHeight <= height){
                            ivTopMargin = ((height - ivHeight) / 2f + top).toInt()
                        }else{
                            ivTopMargin = (top - (ivHeight - height) / 2f).toInt()
                        }

                        ivParams.setMargins(0,ivTopMargin,0,ivLeftMargin)
                        ivDelete.layoutParams = ivParams

                        ivDelete.post {
                            val rvLeftMargin = left+ivDelete.measuredWidth + ivLeftMargin
                            var rvParams : FrameLayout.LayoutParams = rv.layoutParams as FrameLayout.LayoutParams
                            if (isSelect){
                                rvParams.setMargins(rvLeftMargin,0,0,0)
                            }else{
                                rvParams.setMargins(0,0,0,0)
                            }
                            rv.layoutParams = rvParams

//                            ivDrag?.let { ivDrag->
//                                var ivParams:FrameLayout.LayoutParams = ivDrag.layoutParams as FrameLayout.LayoutParams
//                                val ivLeftMargin = ivParams.leftMargin + ivDelete.measuredWidth + ivLeftMargin
//                                ivParams.leftMargin = ivLeftMargin
//                                ivDrag.layoutParams = ivParams
//                            }

                        }

                    }

                    rlSelect?.let { rlSelect->
                        ivDragSelectColumn?.visibility = View.GONE
                        ivDragSelectRow?.visibility = View.VISIBLE
                        if (isSelect){
                            rlSelect.visibility = View.VISIBLE
                        }else{
                            rlSelect.visibility = View.GONE
                        }
                        rv.post {
                            val tableRange = it.getSelectRowRange()
                            val rlWidth = tableRange.width
                            val rlHeight = 30
                            val rlLeft = rv.marginLeft
                            val rlTop = (rv.marginTop + top + (height- rlHeight / 2f)).toInt()
                            var rlParams:FrameLayout.LayoutParams = rlSelect.layoutParams as FrameLayout.LayoutParams
                            rlParams.width = rlWidth
                            rlParams.height = rlHeight
                            rlParams.setMargins(rlLeft,rlTop,0,0)
                            rlSelect.layoutParams = rlParams
                        }
                    }
                }

                //选中中间表格
                it.setOnSelectContentItem { isSelect, width, height, left, top ->
                    setNoSelected()
                }

                //全部不选中
                it.setOnNoSelected {
                    setNoSelected()
                }

                /**
                 * 自定义数据
                 */
                val columnTextList = LetterUtil.generateCombinations(column / 26 + 1)
                for (i in 0 until row*column){
                    var calRow = 0
                    var calCol = 0
                    if (i < column){
                        calRow = 0
                        calCol = i
                    }else{
                        calRow = (i / column)
                        calCol = (i % column)
                    }
                    if (calRow == 0 && calCol == 0){
                        list.add(TableBean(
                            100,100,"",calRow,calCol
                        ))
                    } else if (calRow == 0 && calCol in 1 until column-1 ){
                        //第一行
                        val text = columnTextList[calCol - 1]
                        list.add(TableBean(
                            100,100,"$text",calRow,calCol
                        ))
                    }else if (calCol == 0 && calRow in 1 until row-1 ){
                        //第一列
                        list.add(TableBean(
                            100,100,"$calRow",calRow,calCol
                        ))
                    }else{
                        if (calCol == column - 1 || calRow == row - 1 ){
                            //最后一行和最后一列
                            list.add(TableBean(
                                100,100,"",calRow,calCol
                            ))
                        }else{
                            //中间表格数据
                            list.add(TableBean(
                                100,100,"$calRow/$calCol",calRow,calCol
                            ))
                        }
                    }

                }

                it.setList(list)

                it.setOnRowAddItem {
                    setNoSelected()
                    addRow(it,rv)
                }

                it.setOnColumnAddItem {
                    setNoSelected()
                    addColumn(it,rv)
                }

            }

        }

    }

//    @SuppressLint("ClickableViewAccessibility")
//    private fun initIvDrag(){
//        ivDrag = findViewById(R.id.iv_drag)
//        ivDrag?.setOnTouchListener { v, event ->
//            when (event.action) {
//                MotionEvent.ACTION_DOWN -> {
//                    // 记录初始触摸位置
//                    dX = v.x - event.rawX
//                    dY = v.y - event.rawY
//
//                    // 初始化 lastX 和 lastY
//                    lastX = event.rawX
//                    lastY = event.rawY
//                }
//
//                MotionEvent.ACTION_MOVE ->    {// 更新ImageView的位置
//
//                    // 计算移动的距离
//                    var deltaX = event.rawX - lastX
//                    var deltaY = event.rawY - lastY
//
////                    Log.e("iv","rawX=${event.rawX} rawY=${event.rawY} deltaX=$deltaX deltaY=$deltaY")
//
//                    adapter?.let { adapter->
//
//                        if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
//                            val selectedColumnWidth = adapter.getSelectedColumnWidth()
//                            val minWidth = 50
//                            if (selectedColumnWidth == minWidth && deltaX <= 0) return@let
//                            if (selectedColumnWidth + deltaX <= minWidth){
//                                deltaX = minWidth.toFloat() - selectedColumnWidth
//                            }
//                        }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
//                            val selectedRowHeight = adapter.getSelectedRowHeight()
//                            val minHeight = 50
//                            if (selectedRowHeight == minHeight && deltaY <= 0) return@let
//                            if (selectedRowHeight + deltaY <= minHeight) {
//                                deltaY = minHeight.toFloat() - selectedRowHeight
//                            }
//                            Log.e("iv","selectedRowHeight=${selectedRowHeight}  deltaY=$deltaY")
//
//                        }else{
//                            return@let
//                        }
//
//                        rv?.let { rv->
//                            var params = rv.layoutParams
//                            if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
//                                params.width += deltaX.toInt()
//                                rv.layoutParams = params
//                                adapter.dragItem(deltaX.toInt(),0)
//                            }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
//                                params.height += deltaY.toInt()
//                                rv.layoutParams = params
//                                Log.e("rv","rv width = ${params.height}")
//                                adapter.dragItem(0,deltaY.toInt())
//                            }
//                        }
//
//
//                        var ivParams:FrameLayout.LayoutParams = v.layoutParams as FrameLayout.LayoutParams
//                        if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
//                            var marginLeft = ivParams.leftMargin + deltaX.toInt()
//                            ivParams.leftMargin = marginLeft
//                        }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
//                            var marginTop = ivParams.topMargin + deltaY.toInt()
//                            ivParams.topMargin = marginTop
//                        }
//                        v.layoutParams = ivParams
//
//
//                        ivDelete?.let {ivDelete->
//                            if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
//                                val selectColumnHeadItem =
//                                    adapter.getSelectedColumnHeadItem() ?: return@let
//                                var ivLeftMargin = ivDelete.marginLeft
//                                var ivParams:FrameLayout.LayoutParams = ivDelete.layoutParams as FrameLayout.LayoutParams
//                                val ivWidth = ivDelete.measuredHeight
//                                val width = selectColumnHeadItem.width
//                                var left = 0
//                                for (table in adapter.data){
//                                    if (table.row == 0 && table.column < selectColumnHeadItem.column){
//                                        left += table.width
//                                    }
//                                }
//
//                                if (ivWidth <= width){
//                                    ivLeftMargin = ((width - ivWidth) / 2f + left).toInt()
//                                }else{
//                                    ivLeftMargin = (left - (ivWidth - width) / 2f).toInt()
//                                }
//                                ivParams.leftMargin = ivLeftMargin
//                                ivDelete.layoutParams = ivParams
//                            }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
//                                val selectRowHeadItem =
//                                    adapter.getSelectedRowHeadItem() ?: return@let
//                                var ivTopMargin = ivDelete.marginTop
//                                var ivParams:FrameLayout.LayoutParams = ivDelete.layoutParams as FrameLayout.LayoutParams
//                                val ivHeight = ivDelete.measuredHeight
//                                val height = selectRowHeadItem.height
//                                var top = 0
//                                for (table in adapter.data){
//                                    if (table.column == 0 && table.row < selectRowHeadItem.row){
//                                        top += table.height
//                                    }
//                                }
//
//                                if (ivHeight <= height){
//                                    ivTopMargin = ((height - ivHeight) / 2f + top).toInt()
//                                }else{
//                                    ivTopMargin = (top - (ivHeight - height) / 2f).toInt()
//                                }
//                                ivParams.topMargin = ivTopMargin
//                                ivDelete.layoutParams = ivParams
//                            }
//                        }
//
//
//
//
//                        // 更新 lastX 和 lastY
//                        lastX = event.rawX
//                        lastY = event.rawY
//                    }
//
//                }
//
//                else ->  false
//            }
//            true
//        }
//    }

    private fun initIvDelete(){
        ivDelete = findViewById(R.id.iv_delete)
        ivDelete?.setOnClickListener {
//            ivDrag?.visibility = View.GONE
            rlSelect?.visibility = View.GONE
            ivDelete?.visibility = View.GONE

            rv?.let { rv->
                val rvParams : FrameLayout.LayoutParams = rv.layoutParams as FrameLayout.LayoutParams
                rvParams.setMargins(0,0,0,0)
                rv.layoutParams = rvParams
            }

            adapter?.let { adapter->
                if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
                    val selectedRow = adapter.getSelectedRow()
                    Log.e("TabViewActivity","selectColumn=${selectedRow}")
                    rv?.let { rv->
                        val rvParams:ViewGroup.LayoutParams = rv.layoutParams
                        rvParams.height = adapter.getHeight() - adapter.data[selectedRow].height
                        rv.layoutParams = rvParams
                        rv.post {
                            adapter.deleteRow(selectedRow)
                        }
                    }

                }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
                    val selectedColumn = adapter.getSelectedColumn()
                    Log.e("TabViewActivity","selectColumn=${selectedColumn}")
                    rv?.let { rv->
                        val rvParams:ViewGroup.LayoutParams = rv.layoutParams
                        rvParams.width = adapter.getWidth() - adapter.data[selectedColumn].width
                        rv.layoutParams = rvParams
                        rv.post {
                            adapter.deleteColumn(selectedColumn)
                        }
                    }
                }
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initRlSelect(){
        ivDragSelectColumn = findViewById(R.id.iv_drag_select_column)
        ivDragSelectRow = findViewById(R.id.iv_drag_select_row)
        rlSelect = findViewById(R.id.rl_select)
        rlSelect?.let { rlSelect->
            rlSelect.setOnTouchListener { v, event ->
                when(event.action){
                    MotionEvent.ACTION_DOWN->{
                        // 记录初始触摸位置
                        dX = v.x - event.rawX
                        dY = v.y - event.rawY

                        // 初始化 lastX 和 lastY
                        lastX = event.rawX
                        lastY = event.rawY
                    }

                    MotionEvent.ACTION_MOVE ->    {
                        // 计算移动的距离
                        var deltaX = event.rawX - lastX
                        var deltaY = event.rawY - lastY

                        adapter?.let { adapter->

                            if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
                                val selectedColumnWidth = adapter.getSelectedColumnWidth()
                                val minWidth = 50
                                if (selectedColumnWidth == minWidth && deltaX <= 0) return@let
                                if (selectedColumnWidth + deltaX <= minWidth){
                                    deltaX = minWidth.toFloat() - selectedColumnWidth
                                }
                            }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
                                val selectedRowHeight = adapter.getSelectedRowHeight()
                                val minHeight = 50
                                if (selectedRowHeight == minHeight && deltaY <= 0) return@let
                                if (selectedRowHeight + deltaY <= minHeight) {
                                    deltaY = minHeight.toFloat() - selectedRowHeight
                                }
                                Log.e("iv","selectedRowHeight=${selectedRowHeight}  deltaY=$deltaY")

                            }else{
                                return@let
                            }

//                            ivDrag?.let { ivDrag->
//                                val ivParams:FrameLayout.LayoutParams = ivDrag.layoutParams as FrameLayout.LayoutParams
//                                ivParams.leftMargin = (ivParams.leftMargin + deltaX).toInt()
//                                ivDrag.layoutParams = ivParams
//                            }

                            rv?.let { rv->
                                var params = rv.layoutParams
                                if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
                                    params.width += deltaX.toInt()
                                    rv.layoutParams = params
                                    adapter.dragItem(deltaX.toInt(),0)
                                }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
                                    params.height += deltaY.toInt()
                                    rv.layoutParams = params
                                    Log.e("rv","rv width = ${params.height}")
                                    adapter.dragItem(0,deltaY.toInt())
                                }
                            }


                            var ivParams:FrameLayout.LayoutParams = v.layoutParams as FrameLayout.LayoutParams
                            if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
                                var marginLeft = ivParams.leftMargin + deltaX.toInt()
                                ivParams.leftMargin = marginLeft
                            }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
                                var marginTop = ivParams.topMargin + deltaY.toInt()
                                ivParams.topMargin = marginTop
                            }
                            v.layoutParams = ivParams


                            ivDelete?.let {ivDelete->
                                if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.COLUMN){
                                    val selectColumnHeadItem =
                                        adapter.getSelectedColumnHeadItem() ?: return@let
                                    var ivLeftMargin = ivDelete.marginLeft
                                    var ivParams:FrameLayout.LayoutParams = ivDelete.layoutParams as FrameLayout.LayoutParams
                                    val ivWidth = ivDelete.measuredHeight
                                    val width = selectColumnHeadItem.width
                                    var left = 0
                                    for (table in adapter.data){
                                        if (table.row == 0 && table.column < selectColumnHeadItem.column){
                                            left += table.width
                                        }
                                    }

                                    if (ivWidth <= width){
                                        ivLeftMargin = ((width - ivWidth) / 2f + left).toInt()
                                    }else{
                                        ivLeftMargin = (left - (ivWidth - width) / 2f).toInt()
                                    }
                                    ivParams.leftMargin = ivLeftMargin
                                    ivDelete.layoutParams = ivParams
                                }else if (adapter.getIsSelectColumnRow() == TableAdapter.selectType.ROW){
                                    val selectRowHeadItem =
                                        adapter.getSelectedRowHeadItem() ?: return@let
                                    var ivTopMargin = ivDelete.marginTop
                                    var ivParams:FrameLayout.LayoutParams = ivDelete.layoutParams as FrameLayout.LayoutParams
                                    val ivHeight = ivDelete.measuredHeight
                                    val height = selectRowHeadItem.height
                                    var top = 0
                                    for (table in adapter.data){
                                        if (table.column == 0 && table.row < selectRowHeadItem.row){
                                            top += table.height
                                        }
                                    }

                                    if (ivHeight <= height){
                                        ivTopMargin = ((height - ivHeight) / 2f + top).toInt()
                                    }else{
                                        ivTopMargin = (top - (ivHeight - height) / 2f).toInt()
                                    }
                                    ivParams.topMargin = ivTopMargin
                                    ivDelete.layoutParams = ivParams
                                }
                            }

                            // 更新 lastX 和 lastY
                            lastX = event.rawX
                            lastY = event.rawY
                        }
                    }

                    else ->  true
                }
                true
            }
        }
    }

    private fun setNoSelected(){
//        ivDrag?.visibility = View.GONE
        Log.e("TableViewActivity","setNoSelected ------------")
        rlSelect?.visibility = View.GONE
        ivDelete?.visibility = View.GONE

        rv?.let { rv->
            val rvParams : FrameLayout.LayoutParams = rv.layoutParams as FrameLayout.LayoutParams
            rvParams.setMargins(0,0,0,0)
            rv.layoutParams = rvParams
        }

    }

    /**
     * 添加列
     */
    private fun addColumn(it:TableAdapter,rv:SwipeRecyclerView){
        val gridWidth = 100
        adapter?.let { adapter ->
            val rvWidth = adapter.getWidth() + gridWidth

            var rvParams = rv.layoutParams
            rvParams.width = rvWidth
            rvParams.height = adapter.getHeight()
            rv.layoutParams = rvParams

            var column = adapter.getColumn()

            val columnHeadList = LetterUtil.generateCombinations(column / 26 + 1)
            var newList : MutableList<TableBean> = arrayListOf()
            var index = 0
            for (table in adapter.data){
                if ( table.column == column - 1){
                    val text = if (table.row == 0){
                        "${columnHeadList[table.column]}"
                    }else{
                        ""
                    }

                    newList.add(TableBean(
                        width = gridWidth,
                        height = table.height,
                        text = text,
                        row = table.row,
                        column = table.column
                    ))

                    val text1 = if (table.row == 0){
                        "${columnHeadList[table.column + 1]}"
                    }else{
                        ""
                    }

                    newList.add(TableBean(
                        width = table.width,
                        height = table.height,
                        text = text1,
                        row = table.row,
                        column = table.column + 1
                    ))
                }else{
                    newList.add(TableBean(
                        width = table.width,
                        height = table.height,
                        text = table.text,
                        row = table.row,
                        column = table.column
                    ))
                }
            }

            this.list = newList
            it.addColumn(rvWidth,newList)

        }

    }

    /**
     * 添加行
     */
    private fun addRow(it:TableAdapter,rv:SwipeRecyclerView){
        val gridHeight = 100
        adapter?.let { adapter->
            val rvHeight = adapter.getHeight() + gridHeight

            var rvParams = rv.layoutParams
            rvParams.width = adapter.getWidth()
            rvParams.height = rvHeight
            rv.layoutParams = rvParams


            var column = adapter.getColumn()
            var row = adapter.getRow()

            var newList : MutableList<TableBean> = arrayListOf()
            for (table in list){
                if ( table.row == row - 2 && table.column == column - 1 ){
                    newList.add(TableBean(
                        width = table.width,
                        height = table.height,
                        text = "",
                        row = table.row,
                        column = table.column
                    ))
                    for (col in 0 until column){
                        val text = if (col == 0){
                            "${table.row + 1}"
                        }else{
                            ""
                        }
                        newList.add(TableBean(
                            width = list[col].width,
                            height = gridHeight,
                            text = text,
                            row = table.row + 1,
                            column = list[col].column
                        ))
                    }
                }else if (table.row == row - 1){
                    newList.add(TableBean(
                        width = table.width,
                        height = table.height,
                        text = table.text,
                        row = table.row + 1,
                        column = table.column
                    ))
                }else{
                    newList.add(TableBean(
                        width = table.width,
                        height = table.height,
                        text = table.text,
                        row = table.row,
                        column = table.column
                    ))
                }
            }
            this.list = newList
            it.addRow(rvHeight,newList)
        }
    }

    /**
     * 生成指定长度的字母组合
     */
    private fun generateCombinations(length: Int): StringBuilder{
        val combination = StringBuilder()
        val max = Math.pow(26.0, length.toDouble()).toInt() // 计算组合总数
        for (i in 0 until max) {
            // 构建字母组合
            for (j in 0 until length) {
                val index = i / Math.pow(26.0, j.toDouble()).toInt() % 26 // 计算当前字母的位置
                combination.insert(0, ('A'.code + index).toChar()) // 将字母插入到组合的最前面
            }

            // 输出当前字母组合
//            Log.e("TabActivity",combination.toString())
        }

        return combination.reverse()
    }

}